// 清除所有缓存还有点问题 TODO，应该是scopeStorageKeys会被设置为空了
export const clearnScopeStorageKeys = () => {
    Array.from(scopeStorageKeys).forEach(k => localStorage.removeItem(k))
}

export const scopeStorage = (scope) => new Proxy({}, {
    get(_, k) {
        let v = localStorage.getItem(k)
        try {
            v = JSON.parse(v)
        } catch (e) {

        }

        return v
    },
    set(_, k, v) {
        scopeStorageKeys.add(k)
        if (typeof v === 'object') {
            v = JSON.stringify(v)
        }
        localStorage.setItem(k, v)
        return true
    }
})
export const storage = scopeStorage()

const scopeStorageKeys = new Set(storage.__)